<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="房间号" prop="sa" v-if="pricePId==null||pricePId==''">
        <!-- <el-input v-model="dataForm.roomId" style="width: 200px;" placeholder="房间号"></el-input>
          <el-input v-model="dataForm.userid" style="width: 200px;" placeholder="用户"></el-input> -->
        <el-autocomplete v-model="dataForm.sa" :fetch-suggestions="querySearchAsync" placeholder="请输入内容" @select="handleSelect"></el-autocomplete>
      </el-form-item>
      <el-form-item label="物品名称" prop="name">
        <el-select v-model="dataForm.name" placeholder="请选择">
          <el-option v-for="item in agreementtype" :key="item.id" :label="item.label" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数量" prop="number">
          <el-input-number v-model="dataForm.number"  :min="1"  label="数量"></el-input-number>
      </el-form-item>
      <el-form-item label="押金" prop="yamoney">
        <el-input type="number" v-model="dataForm.yamoney" placeholder="押金"></el-input>
      </el-form-item>
      <el-form-item label="单据号" prop="danNo">
        <el-input type="number" v-model="dataForm.danNo" placeholder="单据号"></el-input>
      </el-form-item>
      <el-form-item label="租借时间" prop="startTime">
            <el-date-picker
              v-model="dataForm.startTime"
              format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="租借时间">
            </el-date-picker>
<!--        <el-input v-model="dataForm.startTime" placeholder=""></el-input> -->
      </el-form-item>
      <el-form-item label="备注" prop="remake">
        <el-input type="textarea" v-model="dataForm.remake" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
  props:{
    foomFloorId:{
      type:String
    },
    pricePId:{
      type:String
    }
  },
    data() {
      return {
        visible: false,
        agreementtype: [],
        dataForm: {
          userid: '',
          id: 0,
          name: '',
          number: '',
          yamoney: '',
          danNo: '',
           sa:'',
          startTime:new Date(),
          fjr:'',
          remake: '',
          roomId: '',
          status: ''
        },
        restaurants: [],
        dataRule: {
          name: [{
            required: true,
            message: '物品名称不能为空',
            trigger: 'change'
          }],
          number: [{
            required: true,
            message: '数量不能为空',
            trigger: 'blur'
          }],
          yamoney: [{
            required: true,
            message: '押金不能为空',
            trigger: 'blur'
          }],
          danNo: [{
            required: true,
            message: '单据号不能为空',
            trigger: 'blur'
          }],
          startTime: [{
            required: true,
            message: '租借时间不能为空',
            trigger: 'blur'
          }],
          sa: [{
            required: true,
            message: '请选择房间',
            trigger: 'change'
          }]


        }
      }
    },
    created() {

      this.getagreementType('zjwp');
      this.getList();
      if(this.pricePId!=null||this.pricePId!=''){
           this.dataForm.fjr=this.foomFloorId+"/"+this.pricePId
      }
    },
    methods: {
      handleSelect(item) {
         this.dataForm.fjr=item.id;
      },
      querySearchAsync(queryString, cb) {

        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;

        clearTimeout(this.timeout);
          cb(results);
      },
      createStateFilter(queryString) {
        return (state) => {
          return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      getList() {

        this.$http({
          url: this.$http.adornUrl('/hotel/trentalgoods/selectList'),
          method: 'post',
        }).then(({
          data
        }) => {

          this.restaurants = data.page;

        });

      },
      getagreementType(type) {
        this.$http({
          url: this.$http.adornUrl('/hotel/thotelagreementtype/list'),
          method: 'get',
          params: this.$http.adornParams({
            page: 1,
            limit: 999,
            type: type
          })
        }).then(({
          data
        }) => {

          this.agreementtype = data.page.list;

        });
      },

      getTime(){
        var year =this.dataForm.startTime.getFullYear();//获取完整的年份(4位,1970-????)
                        var month = this.dataForm.startTime.getMonth() + 1;//获取当前月份(0-11,0代表1月)
                        var day = this.dataForm.startTime.getDate();//获取当前日(1-31)
                        if (month < 10) {
                             month ="0" + month;
                         }
                        if (day < 10) {
                             day ="0" + day;
                         }
        var h= this.dataForm.startTime.getHours();       // 获取当前小时数(0-23)
         var s=this.dataForm.startTime.getMinutes();     // 获取当前分钟数(0-59)
         var a=this.dataForm.startTime.getSeconds();
          if (h < 10) {
                              h ="0" + h;
                          }
                         if (s < 10) {
                              s ="0" + s;
                          }
          if (a < 10) {
                              a ="0" + a;
                          }
                       
         var dateString = year +"-" + month + "-" + day  +"  "+h+":"+s+":"+a;

         return dateString;
      },
      init(id) {
        this.getList();
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/trentalgoods/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tRentalGoods.name
                this.dataForm.number = data.tRentalGoods.number
                this.dataForm.yamoney = data.tRentalGoods.yamoney
                this.dataForm.danNo = data.tRentalGoods.danNo
                this.dataForm.startTime = data.tRentalGoods.startTime
                this.dataForm.remake = data.tRentalGoods.remake
                this.dataForm.roomId = data.tRentalGoods.roomId
                this.dataForm.endTime = data.tRentalGoods.endTime
                this.dataForm.status = data.tRentalGoods.status
                this.dataForm.sa= data.tRentalGoods.sa
				this.dataForm.fjr= data.tRentalGoods.fjr
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        if(this.dataForm.startTime.constructor==Date){
          this.dataForm.startTime=this.getTime()
        }


        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/trentalgoods/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'number': this.dataForm.number,
                'yamoney': this.dataForm.yamoney,
                'danNo': this.dataForm.danNo,
                'startTime': this.dataForm.startTime,
                'remake': this.dataForm.remake,
                'fjr':this.dataForm.fjr,
                'roomId': this.dataForm.roomId,
                'endTime': this.dataForm.endTime,
                'status': this.dataForm.status
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.el-dialog__wrapper /deep/ .el-dialog {
  width: 30%;
  .el-textarea__inner {
    height: 100px;
  }
}
@media only screen and (max-width: 1180px) {
  .el-dialog__wrapper /deep/ .el-dialog {
    width: 40%;
  }
}
</style>
